package com.ssm.bean;

import lombok.Data;

import java.io.Serializable;
@Data
public class Result<T> implements Serializable {

    private Result() {}; //无参构造为private，不让外部使用无参构造，都要遵循我们自己规定的构造方法
    private Boolean success; //是否成功

    private Integer code; //状态码(非网络层状态码)

    private String message; //请求信息(成功/失败)

    private T data; //请求结果(类型不唯一、使用泛型T，先在类中声明T)

    public static Result ok() { //成功
        Result result = new Result();
        result.setSuccess(true);
        result.setCode(ResultCode.SUCCESS);
        result.setMessage(ResultMessage.SUCCESS);
        return result;
    }

    public static Result ok(Integer resultCode, String resultMessage) { //成功，参数由外部传入
        Result result = new Result();
        result.setSuccess(true);
        result.setCode(resultCode);
        result.setMessage(resultMessage);
        return result;
    }

    public static<T> Result ok(Integer resultCode, String resultMessage, T data) { //成功，参数由外部传入，有返回值(在static后声明泛型T)
        Result result = new Result();
        result.setSuccess(true);
        result.setCode(resultCode);
        result.setMessage(resultMessage);
        result.setData(data);
        return result;
    }

    public static<T> Result ok(T data) { //成功,有返回值(在static后声明泛型T)
        Result result = new Result();
        result.setSuccess(true);
        result.setCode(ResultCode.SUCCESS);
        result.setMessage(ResultMessage.SUCCESS);
        result.setData(data);
        return result;
    }

    public static Result fail() { //失败
        Result result = new Result();
        result.setSuccess(true);
        result.setCode(ResultCode.ERROR);
        result.setMessage(ResultMessage.ERROR);
        return result;
    }

    public static Result fail(Integer resultCode, String resultMessage) { //失败，参数由外部传入
        Result result = new Result();
        result.setSuccess(true);
        result.setCode(resultCode);
        result.setMessage(resultMessage);
        return result;
    }

    public static<T> Result fail(Integer resultCode, String resultMessage, T data) { //失败，参数由外部传入，有返回值(在static后声明泛型T)
        Result result = new Result();
        result.setSuccess(true);
        result.setCode(resultCode);
        result.setMessage(resultMessage);
        result.setData(data);
        return result;
    }

    public static<T> Result fail(T data) { //失败,有返回值(在static后声明泛型T)
        Result result = new Result();
        result.setSuccess(true);
        result.setCode(ResultCode.ERROR);
        result.setMessage(ResultMessage.ERROR);
        result.setData(data);
        return result;
    }
}
